package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.salesclient.di.component.DaggerGuKeDetailComponent;
import com.xiaoxing.salesclient.di.module.GuKeDetailModule;
import com.xiaoxing.salesclient.mvp.contract.GuKeDetailContract;
import com.xiaoxing.salesclient.mvp.model.entity.ProcessDetail;
import com.xiaoxing.salesclient.mvp.model.entity.UpdateOrder;
import com.xiaoxing.salesclient.mvp.presenter.GuKeDetailPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_GU_KE_DETAIL_ACTIVITY)
public class GuKeDetailActivity extends BaseActivity<GuKeDetailPresenter> implements GuKeDetailContract.View {

    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.tv_shi_jian_value)
    TextView tv_shi_jian_value;
    @BindView(R2.id.tv_shi_ji_shi_jian_value)
    TextView tv_shi_ji_shi_jian_value;
    @BindView(R2.id.tv_zi_xun_ke_ren_ming_zi)
    TextView tv_zi_xun_ke_ren_ming_zi;
    @BindView(R2.id.tv_zi_xun_shi_jian_fan_wei)
    TextView tv_zi_xun_shi_jian_fan_wei;
    @BindView(R2.id.tv_deng_dai_ke_ren_ming_zi)
    TextView tv_deng_dai_ke_ren_ming_zi;
    @BindView(R2.id.tv_deng_dai_shi_jian_fan_wei)
    TextView tv_deng_dai_shi_jian_fan_wei;
    @BindView(R2.id.tv_ke_ren_ming_zi)
    TextView tv_ke_ren_ming_zi;
    @BindView(R2.id.tv_shi_jian_fan_wei)
    TextView tv_shi_jian_fan_wei;
    @BindView(R2.id.tv_lao_cheng_ke_ren_ming_zi)
    TextView tv_lao_cheng_ke_ren_ming_zi;
    @BindView(R2.id.tv_lao_cheng_shi_jian)
    TextView tv_lao_cheng_shi_jian;
    @BindView(R2.id.tv_song_ke_ren_ming_zi)
    TextView tv_song_ke_ren_ming_zi;
    @BindView(R2.id.tv_song_ke_shi_jian)
    TextView tv_song_ke_shi_jian;
    @BindView(R2.id.tv_xia_ci_yu_yue_shi_jian)
    TextView tv_xia_ci_yu_yue_shi_jian;


    @BindView(R2.id.btnRight)
    ImageView btnRight;
    @BindView(R2.id.ll_jie_dai_liu_cheng_cb)
    LinearLayout ll_jie_dai_liu_cheng_cb;
    @BindView(R2.id.ll_zi_xun)
    LinearLayout ll_zi_xun;
    @BindView(R2.id.ll_deng_dai)
    LinearLayout ll_deng_dai;
    @BindView(R2.id.ll_liao_cheng)
    LinearLayout ll_liao_cheng;
    @BindView(R2.id.ll_song_ke)
    LinearLayout ll_song_ke;


    @BindView(R2.id.rl_zi_xun)
    RelativeLayout rl_zi_xun;
    @BindView(R2.id.rl_deng_dai)
    RelativeLayout rl_deng_dai;
    @BindView(R2.id.rl_jie_dai_liu_cheng)
    RelativeLayout rl_jie_dai_liu_cheng;
    @BindView(R2.id.rl_liao_cheng)
    RelativeLayout rl_liao_cheng;
    @BindView(R2.id.rl_song_ke)
    RelativeLayout rl_song_ke;
    @BindView(R2.id.rl_yu_yue_shi_jian)
    RelativeLayout rl_yu_yue_shi_jian;
    @BindView(R2.id.rl_xia_ci_yu_yue)
    RelativeLayout rl_xia_ci_yu_yue;
    @BindView(R2.id.rl_xi_tong_ping_fen)
    RelativeLayout rl_xi_tong_ping_fen;


    @BindView(R2.id.view_line_zi_xun)
    View view_line_zi_xun;
    @BindView(R2.id.view_line_deng_dai)
    View view_line_deng_dai;
    @BindView(R2.id.view_line_jie_dai_liu_cheng)
    View view_line_jie_dai_liu_cheng;
    @BindView(R2.id.view_line_liao_cheng)
    View view_line_liao_cheng;
    @BindView(R2.id.view_line_song_ke)
    View view_line_song_ke;
    @BindView(R2.id.view_line_ben_ci_yu_yue_shi_jian)
    View view_line_ben_ci_yu_yue_shi_jian;
    @BindView(R2.id.view_ling_xia_ci_yu_yue)
    View view_ling_xia_ci_yu_yue;
    @BindView(R2.id.view_line_xi_tong_ji_fen)
    View view_line_xi_tong_ji_fen;

    @Autowired
    String order_id;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.img_ge_ren_zi_liao)
    TextView imgGeRenZiLiao;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerGuKeDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .guKeDetailModule(new GuKeDetailModule(this))
                .build()
                .inject(this);
        ARouter.getInstance().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_gu_ke_detail; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }


    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "顧客詳情");
        btnRight.setImageDrawable(getResources().getDrawable(R.drawable.icon_qie_huan_jue_se));
        btnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Utils.navigation(GuKeDetailActivity.this, RouterHub.SALES_CLIENT_XUAN_ZE_FU_WU_REN_YUAN_ACTIVITY);
                Bundle bundle = new Bundle();
                bundle.putString("order_id_zhuan_jie", order_id);
                Intent intent = new Intent(GuKeDetailActivity.this, XuanZeFuWuRenYuanActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
//                ARouter.getInstance().build(RouterHub.SALES_CLIENT_XUAN_ZE_FU_WU_REN_YUAN_ACTIVITY).withBundle("order_id_zhuan_jie", bundle).navigation();
            }
        });

        mPresenter.processdetail(mSharedPreferencesHelper.getString(BaseConstants.TOKEN), order_id);


        setLineHeight(rl_yu_yue_shi_jian, view_line_ben_ci_yu_yue_shi_jian);
        setLineHeight(rl_xia_ci_yu_yue, view_ling_xia_ci_yu_yue);


    }

    /**
     * 动态设置左侧view的高度
     *
     * @param rl
     * @param view_line
     */
    private static void setLineHeight(RelativeLayout rl, View view_line) {
        rl.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        //只需要获取一次高度，获取后移除监听器
                        rl.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        //这里高度应该定义为成员变量，定义为局部为展示代码方便

                        RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) view_line.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20

                        linearParams.height = rl.getHeight();// 控件的宽强制设成30

                        view_line.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
                    }

                });
    }

    @Override
    public void showLoading() {
        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtil.dismissDialog();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    ProcessDetail.DataBean.InfoBean infoBean;
    @Override
    public void processdetailSuccessed(ProcessDetail processDetail) {

         infoBean = processDetail.getData().getInfo();

//        tv_shi_jian_value.setText(TextUtils.isEmpty(infoBean.getAppoint_date()) ? "無預約" : ArmsUtils.stampToDate(infoBean.getAppoint_date()));
        tv_shi_jian_value.setText(infoBean.getAppoint());
        tvName.setText(infoBean.getNick_name());
//        tv_shi_ji_shi_jian_value.setText(TextUtils.isEmpty(infoBean.getAppoint_time()) ? "" : ArmsUtils.stampToDate(infoBean.getAppoint_time()));
//        tv_shi_ji_shi_jian_value.setText(ArmsUtils.getDataTime("yyyy-MM-dd HH:mm:ss"));
        tv_shi_ji_shi_jian_value.setText(TextUtils.isEmpty(infoBean.getCreate_time()) ? "無預約" : ArmsUtils.stampToDate(infoBean.getCreate_time()));

        tv_xia_ci_yu_yue_shi_jian.setText(ArmsUtils.stampToDate(infoBean.getCreate_time()));


        if (processDetail != null && processDetail.getData() != null && processDetail.getData().getProcess() != null) {


            if (processDetail.getData().getProcess().get(0) != null && processDetail.getData().getProcess().get(0).getChild() != null && processDetail.getData().getProcess().get(0).getChild().size() > 0) {

                tv_zi_xun_ke_ren_ming_zi.setText(processDetail.getData().getProcess().get(0).getTransfer_staff());
                tv_zi_xun_shi_jian_fan_wei.setText(TextUtils.isEmpty(processDetail.getData().getProcess().get(0).getCreate_time()) ? "無預約" : ArmsUtils.stampToDate(processDetail.getData().getProcess().get(0).getCreate_time()));
                tv_zi_xun_shi_jian_fan_wei.setVisibility(View.GONE);
                List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
                // 给指定的checkbox赋值
                for (int i = 0; i < processDetail.getData().getProcess().get(0).getChild().size(); i++) {
                    // 先获得checkbox.xml的对象
                    CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(
                            R.layout.gu_ke_detail_checkbox, null);
                    checkBoxs.add(checkBox);
                    if (processDetail.getData().getProcess().get(0).getChild().get(i).getStatus() == 1) {
                        checkBox.setChecked(true);
                    }
                    int finalI = i;
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            updateorder(processDetail.getData().getProcess().get(0).getChild().get(finalI).getCate_id());

                        }
                    });
                    checkBoxs.get(i).setText(processDetail.getData().getProcess().get(0).getChild().get(i).getCate_name());
                    // 实现了在
                    ll_zi_xun.addView(checkBox, i);
                }

                setLineHeight(rl_zi_xun, view_line_zi_xun);

            }

            if (processDetail.getData().getProcess().get(1) != null && processDetail.getData().getProcess().get(1).getChild() != null && processDetail.getData().getProcess().get(1).getChild().size() > 0) {

                tv_deng_dai_ke_ren_ming_zi.setText(processDetail.getData().getProcess().get(1).getTransfer_staff());
                tv_deng_dai_shi_jian_fan_wei.setText(TextUtils.isEmpty(processDetail.getData().getProcess().get(1).getCreate_time()) ? "無預約" : ArmsUtils.stampToDate(processDetail.getData().getProcess().get(1).getCreate_time()));
                tv_deng_dai_shi_jian_fan_wei.setVisibility(View.GONE);
                List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
                // 给指定的checkbox赋值
                for (int i = 0; i < processDetail.getData().getProcess().get(1).getChild().size(); i++) {
                    // 先获得checkbox.xml的对象
                    CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(
                            R.layout.gu_ke_detail_checkbox, null);
                    checkBoxs.add(checkBox);
                    if (processDetail.getData().getProcess().get(1).getChild().get(i).getStatus() == 1) {
                        checkBox.setChecked(true);
                    }
                    int finalI = i;
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            updateorder(processDetail.getData().getProcess().get(1).getChild().get(finalI).getCate_id());

                        }
                    });
                    checkBoxs.get(i).setText(processDetail.getData().getProcess().get(1).getChild().get(i).getCate_name());
                    // 实现了在
                    ll_deng_dai.addView(checkBox, i);
                }
                setLineHeight(rl_deng_dai, view_line_deng_dai);

            }

            if (processDetail.getData().getProcess().get(2) != null && processDetail.getData().getProcess().get(2).getChild() != null && processDetail.getData().getProcess().get(2).getChild().size() > 0) {

                tv_ke_ren_ming_zi.setText(processDetail.getData().getProcess().get(2).getTransfer_staff());
                tv_shi_jian_fan_wei.setText(TextUtils.isEmpty(processDetail.getData().getProcess().get(2).getCreate_time()) ? "無預約" : ArmsUtils.stampToDate(processDetail.getData().getProcess().get(2).getCreate_time()));
                tv_shi_jian_fan_wei.setVisibility(View.GONE);
                List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
                // 给指定的checkbox赋值
                for (int i = 0; i < processDetail.getData().getProcess().get(2).getChild().size(); i++) {
                    // 先获得checkbox.xml的对象
                    CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(
                            R.layout.gu_ke_detail_checkbox, null);
                    checkBoxs.add(checkBox);
                    if (processDetail.getData().getProcess().get(2).getChild().get(i).getStatus() == 1) {
                        checkBox.setChecked(true);
                    }
                    int finalI = i;
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            updateorder(processDetail.getData().getProcess().get(2).getChild().get(finalI).getCate_id());

                        }
                    });
                    checkBoxs.get(i).setText(processDetail.getData().getProcess().get(2).getChild().get(i).getCate_name());
                    // 实现了在
                    ll_jie_dai_liu_cheng_cb.addView(checkBox, i);
                }
                setLineHeight(rl_jie_dai_liu_cheng, view_line_jie_dai_liu_cheng);
            }
            if (processDetail.getData().getProcess().get(3) != null && processDetail.getData().getProcess().get(3).getChild() != null && processDetail.getData().getProcess().get(3).getChild().size() > 0) {

                tv_lao_cheng_ke_ren_ming_zi.setText(processDetail.getData().getProcess().get(3).getTransfer_staff());
                tv_lao_cheng_shi_jian.setText(TextUtils.isEmpty(processDetail.getData().getProcess().get(3).getCreate_time()) ? "無預約" : ArmsUtils.stampToDate(processDetail.getData().getProcess().get(3).getCreate_time()));
                tv_lao_cheng_shi_jian.setVisibility(View.GONE);
                List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
                // 给指定的checkbox赋值
                for (int i = 0; i < processDetail.getData().getProcess().get(3).getChild().size(); i++) {
                    // 先获得checkbox.xml的对象
                    CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(
                            R.layout.gu_ke_detail_checkbox, null);
                    checkBoxs.add(checkBox);
                    if (processDetail.getData().getProcess().get(3).getChild().get(i).getStatus() == 1) {
                        checkBox.setChecked(true);
                    }
                    int finalI = i;
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            updateorder(processDetail.getData().getProcess().get(3).getChild().get(finalI).getCate_id());

                        }
                    });
                    checkBoxs.get(i).setText(processDetail.getData().getProcess().get(3).getChild().get(i).getCate_name());
                    // 实现了在
                    ll_liao_cheng.addView(checkBox, i);
                }
                setLineHeight(rl_liao_cheng, view_line_liao_cheng);

            }
            if (processDetail.getData().getProcess().get(4) != null && processDetail.getData().getProcess().get(4).getChild() != null && processDetail.getData().getProcess().get(4).getChild().size() > 0) {

                tv_song_ke_ren_ming_zi.setText(processDetail.getData().getProcess().get(4).getTransfer_staff());
                tv_song_ke_shi_jian.setText(TextUtils.isEmpty(processDetail.getData().getProcess().get(4).getCreate_time()) ? "無預約" : ArmsUtils.stampToDate(processDetail.getData().getProcess().get(4).getCreate_time()));
                tv_song_ke_shi_jian.setVisibility(View.GONE);

                List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
                // 给指定的checkbox赋值
                for (int i = 0; i < processDetail.getData().getProcess().get(4).getChild().size(); i++) {
                    // 先获得checkbox.xml的对象
                    CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(
                            R.layout.gu_ke_detail_checkbox, null);
                    checkBoxs.add(checkBox);
                    if (processDetail.getData().getProcess().get(4).getChild().get(i).getStatus() == 1) {
                        checkBox.setChecked(true);
                    }
                    int finalI = i;
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                            updateorder(processDetail.getData().getProcess().get(4).getChild().get(finalI).getCate_id());

                        }
                    });
                    checkBoxs.get(i).setText(processDetail.getData().getProcess().get(4).getChild().get(i).getCate_name());
                    // 实现了在
                    ll_song_ke.addView(checkBox, i);
                }
                setLineHeight(rl_song_ke, view_line_song_ke);

            }

        }

    }

    private void updateorder(String cate_id) {
        mPresenter.updateOrder(mSharedPreferencesHelper.getString(BaseConstants.TOKEN), order_id, cate_id);
    }

    @Override
    public void updateorderSuccessed(UpdateOrder updateOrder) {
//        ArmsUtils.snackbarText(updateOrder.getMsg());
        ArmsUtils.snackbarText("服務成功");
    }


    @OnClick({R2.id.img_yu_yue, R2.id.tv_yu_yue})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.img_yu_yue) {
            Utils.navigation(GuKeDetailActivity.this, RouterHub.SALES_CLIENT_YU_YUE_CHA_XUN_ACTIVITY);
        } else if (i == R.id.tv_yu_yue) {
            Utils.navigation(GuKeDetailActivity.this, RouterHub.SALES_CLIENT_YU_YUE_CHA_XUN_ACTIVITY);

        }
    }


    @OnClick(R2.id.img_ge_ren_zi_liao)
    public void geRenZiLiao() {
        ARouter.getInstance().build(RouterHub.SALES_CLIENT_GE_REN_ZHONG_XIN_ACTIVITY).withString("uid", infoBean.getUid()).navigation();
    }
}
